package com.iwhalecloud.bss.kite.cucc.service.account;

import java.util.*;

import com.iwhalecloud.bss.hsf.baseinf.service.InfExchangeService;
import com.iwhalecloud.bss.kite.cart.common.constant.KiteCartInfConsts;
import com.iwhalecloud.bss.kite.cart.service.util.KiteCartShoppingCartUtil;
import com.iwhalecloud.bss.kite.common.callclient.service.IOrderQueryService;
import com.iwhalecloud.bss.kite.common.entity.InfService;
import com.iwhalecloud.bss.kite.common.util.*;
import com.iwhalecloud.bss.kite.cucc.client.api.inf.IInfServiceBus;
import com.iwhalecloud.bss.kite.cucc.client.dto.account.*;
import com.iwhalecloud.bss.kite.cucc.common.callclient.HttpsCallClient;
import com.iwhalecloud.bss.kite.cucc.service.inf.InfServiceHelper;
import com.iwhalecloud.bss.kite.dataservice.cache.DcPublicCache;
import com.ztesoft.bss.common.util.SpringUtil;
import com.ztesoft.bss.provinces.chongqing.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.cucc.client.api.account.ILocalAccountService;
import com.iwhalecloud.bss.kite.cucc.client.dto.cust.CuccContextCustDTO;
import com.iwhalecloud.bss.kite.cucc.common.callclient.constant.CallClientEnum;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalCallClientEnum;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalInfConsts;
import com.iwhalecloud.bss.kite.cucc.service.util.LocalTokenUtil;
import com.ztesoft.bss.common.bo.LoginInfo;
import com.ztesoft.bss.common.util.ContextUtil;

@Service
public class LocalAccountService implements ILocalAccountService {
    @Value("${interface.call.mode:hsf}")
    private String mode;

    @Autowired
    private IInfServiceBus infServiceBus;

    @Override
    public List<AccountDTO> listAccountInfoBySerialNumber(String serialNumber, String authorization) {
        return null;
    }

    @Override
    public PageInfo<QryCustAccountReq> listCustAccountInfo(String custId, String payName, String acountType,
                                                           String acctId, String cbssAcountCode, String provinceAcctId, String payCompactNo, String provinceCode, String eparchyCode,
                                                           String userCode, int pageNum, int pageSize) {
        Map<String, Object> uniBssBody = new HashMap<>();
        Map<String, Object> qryCustAccountListReq = new HashMap<>();
        Map<String, String> tcpCont = new HashMap<>();
        //当APP_KEY为Z时,接口增加返回"是否总部直签"
        String headSign = DcPublicCache.getCodea("20201216002", "qryAccountInfo");
        if (KiteStringUtils.equals("T", headSign)) {
            tcpCont.put("APP_KEY", LocalInfConsts.APP_ID_Z);
        } else {
            tcpCont.put("APP_KEY", LocalInfConsts.APP_ID);
        }
        tcpCont.put("TRANSACION_ID", LocalTokenUtil.getTransId());
        qryCustAccountListReq.put("TCP_CONT", tcpCont);

        Map<String, Object> operUserInfo = new HashMap<>();
//        operUserInfo.put("STAFF_ID", "");
        if (KiteStringUtils.isEmpty(userCode)) {
            operUserInfo.put("USER_CODE", KiteStringUtils.valueOf(ContextUtil.getSimpleLoginInfo().getUserCode())); //必填 用户账号
        } else {
            operUserInfo.put("USER_CODE", userCode);
        }
//        operUserInfo.put("ROLE_ID", "");
//        operUserInfo.put("LOGIN_ROLE_AREA_CODE", "");
        qryCustAccountListReq.put("OPER_USER_INFO", operUserInfo);

        qryCustAccountListReq.put("PAGE_SIZE", pageSize); //必填
        qryCustAccountListReq.put("PAGE_NUM", pageNum); //必填
        if (!KiteStringUtils.isBlank(custId)) {
            qryCustAccountListReq.put("CUST_ID", Long.valueOf(custId));
        }
        qryCustAccountListReq.put("ACCT_ID", acctId);
        qryCustAccountListReq.put("ACOUNT_NAME", payName);
        qryCustAccountListReq.put("ACOUNT_TYPE", acountType);
        qryCustAccountListReq.put("CBSS_ACOUNT_CODE", cbssAcountCode);
        qryCustAccountListReq.put("PROVICE_ACOUNT_CODE", provinceAcctId);
        qryCustAccountListReq.put("PAY_COMPACT_NO", payCompactNo);
        qryCustAccountListReq.put("PROVINCE_CODE", provinceCode); //必填
        //行业务受理查询账户48,49工号不传地市编码
        if(KiteStringUtils.isNotEmpty(provinceCode)&&!provinceCode.startsWith("48")&&!provinceCode.startsWith("49")){
            qryCustAccountListReq.put("EPARCHY_CODE", eparchyCode);
        }
        uniBssBody.put("QRY_CUST_ACCOUNT_LIST_REQ", qryCustAccountListReq);
        String rsp = HttpsCallClient.postStringWithObject(CallClientEnum.QRY_CUSTOMER_ACCTLIST, LocalTokenUtil.getParamMap(uniBssBody), LocalTokenUtil.getHeaderMap());
        PageInfo<QryCustAccountReq> custAccountList = LocalTokenUtil.checkRespParamAndGetDataListPage(rsp, "QRY_CUST_ACCOUNT_LIST_RSP", QryCustAccountReq.class, pageSize, pageNum);
        return custAccountList;
    }

    @Override
    public Map<String, Object> addAccount(CuccContextCustDTO custDto, String payName, String accountType,
                                          String accountCycle, String payCycle, String acctContactName, String acctContactPhone, String acctContacatEmail,
                                          String acctContactAddr, String bindPostAddr, String isCenterDirectSign, String billType,
                                          String billCycle, String billContent, String billEmail, String faxNumber, String recipientName,
                                          String billAddress, String postCode, String remark, String bankAcctName, String bankAcctNo,
                                          String bankCode, String superBankCode, String limitMode, String limitMoney, String agreementNo,
                                          String fdContactName, String fdContactPhone) {

        LoginInfo loginInfo = ContextUtil.getLoginInfo();

        Map<String, Object> param = new HashMap<>();
        CustAccountDTO custAccountDTO = new CustAccountDTO();

        custAccountDTO.setCustId(Long.valueOf(custDto.getCustId())); // 必填
        custAccountDTO.setGroupId(custDto.getGroupId());
        custAccountDTO.setCustName(custDto.getCustName());
        custAccountDTO.setPayName(payName);
        custAccountDTO.setPayModeCode(accountType);
        custAccountDTO.setIsGroupAcct("1");
        //所属区域
        // custDto获取不到时取登录信息省份地市信息
        if (KiteStringUtils.isEmpty(custDto.getProvinceCode())) {
            custAccountDTO.setProvinceCode(loginInfo.getUserInfo().getPostProvinceNbr());
        } else {
            custAccountDTO.setProvinceCode(custDto.getProvinceCode());
        }
        if (KiteStringUtils.isEmpty(custDto.getEparchyCode())) {
            custAccountDTO.setEparchyCode(loginInfo.getUserInfo().getPostLanId());
        } else {
            custAccountDTO.setEparchyCode(custDto.getEparchyCode());
        }
        custAccountDTO.setAccountCycle(accountCycle);
        custAccountDTO.setPayCycle(payCycle);
        custAccountDTO.setAcctContactName(acctContactName);
        custAccountDTO.setAcctContactPhone(acctContactPhone);
        custAccountDTO.setAcctContactEmail(acctContacatEmail);
        custAccountDTO.setAcctContactAddr(acctContactAddr);
        custAccountDTO.setIsCenterDirectSign(isCenterDirectSign);
        custAccountDTO.setRemark(remark);

        //是否邮寄为非必填字段，如果前台不选择默认为不邮寄
        bindPostAddr = KiteStringUtils.isNotEmpty(bindPostAddr) ? bindPostAddr : "0";

        custAccountDTO.setBindPostAddr(bindPostAddr);

        Map<String, Object> operUserInfo = new HashMap<>();
        String operNo = (String) loginInfo.getUserInfo().getExtParams().get("account");//传参改为集客工号
        operUserInfo.put("userCode", KiteStringUtils.isNotEmpty(operNo)?operNo:loginInfo.getStaffInfo().getStaffCode());

        Map<String, Object> tcpContMap = new HashMap<>();
        tcpContMap.put("appKey", LocalInfConsts.APP_ID);

        List<CustAccountPosts> custAccountPostList = new ArrayList<>();
        if (KiteStringUtils.equals(bindPostAddr, "1")) {
            CustAccountPosts custAccountPosts = new CustAccountPosts();
            custAccountPosts.setPostType(billType);
            custAccountPosts.setPostPeriod(billCycle);
            custAccountPosts.setPostContent(billContent);
            custAccountPosts.setEmail(billEmail);
            custAccountPosts.setFax(faxNumber);
            custAccountPosts.setRecName(recipientName);
            custAccountPosts.setPostSendAddr(billAddress);
            custAccountPosts.setPostCode(postCode);
            custAccountPostList.add(custAccountPosts);
            param.put("custAccountPosts", custAccountPostList);
        }

        AcctConsign acctConsign = new AcctConsign();
        if (KiteStringUtils.equalsAny(accountType, "3", "4", "D", "E", "F", "G")) {
            acctConsign.setCustId(custDto.getCustId());
            acctConsign.setEparchyCode(custDto.getEparchyCode());
            acctConsign.setPayModeCode("1"); // 账户类型
            acctConsign.setBankAcctName(bankAcctName); // 开户名称
            acctConsign.setBankAcctNo(bankAcctNo); // 银行账号
            acctConsign.setBankCode(bankCode); // 开户银行
            acctConsign.setSuperBankCode(superBankCode); // 所属银行
            acctConsign.setLimitMode(limitMode); // 托收方式
            acctConsign.setLimitMoney(limitMoney); // 托收金额
            acctConsign.setAgreementNo(agreementNo); // 托收协议号
            acctConsign.setFdContactName(fdContactName); // 财务联系人姓名
            acctConsign.setFdContactPhone(fdContactPhone); // 财务联系人电话
            param.put("acctConsign", acctConsign);
        }

        param.put("custAccount", custAccountDTO);
//        param.put("acctConsign", new AcctConsign());
        param.put("operUserInfo", operUserInfo);
        param.put("tcpCont", tcpContMap);

        List<Map<String, Object>> custAccountCreateReqs = new ArrayList<>();
        custAccountCreateReqs.add(param);
        Map<String, Object> custAccountCreateReqsMap = new HashMap<>();
        custAccountCreateReqsMap.put("custAccountCreateReqs", custAccountCreateReqs);
        Map<String, Object> uniBssBodyMap = new HashMap<>();
        uniBssBodyMap.put("custAccountsCreateReq", custAccountCreateReqsMap);
        JSONObject uniBssBody = JsonUtils.transKeyToUpperCaseObject(JSONObject.toJSON(uniBssBodyMap).toString());
        Map<String, Object> request = new HashMap<>();
        request.put("UNI_BSS_BODY", uniBssBody);
        request.put("OP_CODE", "CUST_ACCOUNT_CAPACITY");
//        param.put("OP_CODE","json_jike_natureCustCenter_syncInternationalAcct");
        InfServiceHelper.adaptGray(request, uniBssBody);
        String result;
        if (KiteStringUtils.isEqual(mode, "http")) {
            result = HttpsCallClient
                .postStringWithObject(LocalCallClientEnum.INF_COMMON_URL, request, LocalTokenUtil.getHeaderMap());
        }
        else {
            InfExchangeService infExchangeService = SpringUtil.getBean("infExchangeService");
            result = infExchangeService.exchange(new JSONObject(request));
        }
      /*  String result = HttpsCallClient.postStringWithObject(LocalCallClientEnum.INF_COMMON_URL, request, null);
        if (KiteStringUtils.isBlank(result)) {
            ErrorConsts.BUSI_COMMON_ERROR.throwOut("添加账户返回参数为空");
        }*/
        Map<?, ?> resultMap = LocalTokenUtil.checkRespParamAndGetObject(result, "CUST_ACCOUNT_CREATE_RSP", Map.class);
        if (!KiteStringUtils.equals(KiteMapUtils.getString(resultMap, "RESULT_CODE"), LocalInfConsts.UNI_BSS_BODY_RESULT_SUCCESS)) {
            ErrorConsts.INF_INVOKE_EXCEPTION.throwOut("CUST_ACCOUNT_CAPACITY",KiteMapUtils.getString(resultMap, "RESULT_MSG"));
        }
        JSONArray list = JsonUtils.parseArray(KiteMapUtils.getString(resultMap, "RESULT_OBJECT"));
        return (Map<String, Object>) list.get(0);
    }

    @Override
    public Map<String, Object> modCustAccount(CuccContextCustDTO custDto, String payName, String accountType,
                                              String accountCycle, String payCycle, String acctContactName, String acctContactPhone,
                                              String acctContacatEmail, String acctContactAddr, String bindPostAddr,
                                              String isCenterDirectSign, String billType, String billCycle, String billContent,
                                              String billEmail, String faxNumber, String recipientName, String billAddress,
                                              String postCode, String remark, String acctId, String custId, String groupId, String provinceCode,
                                              String eparchyCode, String bankAcctName, String bankAcctNo, String bankCode, String superBankCode, String limitMode,
                                              String limitMoney, String agreementNo, String fdContactName, String fdContactPhone) {

        Map<String, Object> returnMap = new HashMap<String, Object>();

        LoginInfo loginInfo = ContextUtil.getLoginInfo();
        List custAccountPostsList = new ArrayList();
        Map<String, Object> param = new HashMap<>();

        Map<String, String> custAccountDTOMap = new HashMap<String, String>();
        custAccountDTOMap.put("payName", payName);
        custAccountDTOMap.put("payModeCode", accountType);
        custAccountDTOMap.put("provinceCode", custDto.getProvinceCode());
        custAccountDTOMap.put("eparchyCode", custDto.getEparchyCode());
        custAccountDTOMap.put("accountCycle", accountCycle);
        custAccountDTOMap.put("payCycle", payCycle);
        custAccountDTOMap.put("acctContactName", acctContactName);
        custAccountDTOMap.put("acctContacatEmail", acctContacatEmail);
        custAccountDTOMap.put("acctContactAddr", acctContactAddr);
        //custAccountDTOMap.put("isCenterDirectSign", isCenterDirectSign);
        custAccountDTOMap.put("custId", custId);
        custAccountDTOMap.put("groupId", groupId);
        custAccountDTOMap.put("remark", remark);
        custAccountDTOMap.put("acctId", acctId);

        //是否邮寄为非必填字段，如果前台不选择默认为不邮寄
        bindPostAddr = KiteStringUtils.isNotEmpty(bindPostAddr) ? bindPostAddr : "0";
        custAccountDTOMap.put("bindPostAddr", bindPostAddr);

        //如果选择了邮寄，则有邮件地址列表
        if (KiteStringUtils.equals(bindPostAddr, "1")) {
            Map<String, String> custAccountPostsMap = new HashMap<String, String>();
            custAccountPostsMap.put("postType", billType);
            custAccountPostsMap.put("postPeriod", billCycle);
            custAccountPostsMap.put("email", billEmail);
            custAccountPostsMap.put("fax", faxNumber);
            custAccountPostsMap.put("recName", recipientName);
            custAccountPostsMap.put("postSendAddr", billAddress);
            custAccountPostsMap.put("postCode", postCode);
            custAccountPostsMap.put("postContent", billContent);
            custAccountPostsList.add(custAccountPostsMap);
            param.put("custAccountPosts", custAccountPostsList);
        }
        //托收信息
        AcctConsign acctConsign = new AcctConsign();
        acctConsign.setCustId(custDto.getCustId());
        acctConsign.setEparchyCode(custDto.getEparchyCode());
        acctConsign.setPayModeCode("1"); // 账户类型
        acctConsign.setBankAcctName(bankAcctName); // 开户名称
        acctConsign.setBankAcctNo(bankAcctNo); // 银行账号
        acctConsign.setBankCode(bankCode); // 开户银行
        acctConsign.setSuperBankCode(superBankCode); // 所属银行
        acctConsign.setLimitMode(limitMode); // 托收方式
        acctConsign.setLimitMoney(limitMoney); // 托收金额
        acctConsign.setAgreementNo(agreementNo); // 托收协议号
        acctConsign.setFdContactName(fdContactName); // 财务联系人姓名
        acctConsign.setFdContactPhone(fdContactPhone); // 财务联系人电话
        param.put("acctConsign", acctConsign);

        Map<String, Object> operUserInfo = new HashMap<>();
        operUserInfo.put("userCode", loginInfo.getStaffInfo().getStaffCode());
        operUserInfo.put("staffId", loginInfo.getStaffInfo().getStaffId() + "");

        param.put("operUserInfo", operUserInfo);
        param.put("custAccount", custAccountDTOMap);

        Map<String, Object> reqMap = new HashMap<String, Object>();
        Map<String, Object> uniBssBodyMap = new HashMap<String, Object>();
        uniBssBodyMap.put("modCustAccountReq", param);
        Map requestMap = LocalTokenUtil.getParamMapLowerCase(uniBssBodyMap);

        JSONObject requestObj = JsonUtils.transKeyToUpperCaseObject(JSONObject.toJSON(requestMap).toString());

        String result = HttpsCallClient.postStringWithObject(LocalCallClientEnum.Mod_CustAccountInfo, requestObj, LocalTokenUtil.getHeaderMap());
        if (KiteStringUtils.isBlank(result)) {
            ErrorConsts.BUSI_COMMON_ERROR.throwOut("修改账户返回参数为空");
        }
        Map<?, ?> resultMap = LocalTokenUtil.checkRespParamAndGetObject(result, "MOD_CUST_ACCOUNT_RSP", Map.class);
        if (!KiteStringUtils.equals(KiteMapUtils.getString(resultMap, "RESULT_CODE"), LocalInfConsts.UNI_BSS_BODY_RESULT_SUCCESS)) {
            ErrorConsts.INF_INVOKE_EXCEPTION.throwOut("local-cucc-modCustAccountInfo",KiteMapUtils.getString(resultMap, "RESULT_MSG"));
        }
        returnMap.put("RESULT_CODE", "0000");
        return returnMap;
    }

    @Override
    public PageInfo<QryCustAccountReq> listCustAccountInfoBySign(String custId, String payName, String acountType, String acctId, String cbssAcountCode, String provinceAcctId, String payCompactNo, String provinceCode, String eparchyCode, String userCode, String sign, int pageNum, int pageSize) {
        Map<String, Object> resultMap = infServiceBus.batchQryCustAccounts(sign, custId, provinceCode);
        JSONArray custAccts = KiteMapUtils.getValue(resultMap, "CUST_ACCTS");
        List list = new ArrayList();
        if (KiteListUtils.isNotEmpty(custAccts)) {
            JSONObject custAcct = custAccts.getJSONObject(0);
            String custName = KiteMapUtils.getString(custAcct, "CUST_NAME");
            JSONArray accounts = KiteMapUtils.getValue(custAcct, "ACOUNTS");
            if (KiteListUtils.isNotEmpty(accounts)) {
                accounts.forEach(account -> {
                    Map<String, Object> acctInfo = KiteMapUtils.getValue((JSONObject)account, "ACCT_INFO");
                    acctInfo.put("CUST_NAME", custName);

                    Map<String, Object> acctMap = new HashMap<>();
                    acctMap.put("CUST_ACCOUNT", acctInfo);
                    acctMap.put("ACCT_CONSIGN", KiteMapUtils.getValue((JSONObject)account, "ACCT_CONSIGN"));
                    acctMap.put("CUST_ACCOUNT_POSTS", KiteMapUtils.getValue((JSONObject)account, "CUST_ACCOUNT_POSTS"));
                    QryCustAccountReq custAccount = JsonUtils.parseObject(JSONObject.toJSONString(acctMap), QryCustAccountReq.class);
                    list.add(custAccount);
                });
            }
        }
        Integer total = list.size();
        return PageInfoUtils.coverToPageTotal(list, pageNum, pageSize, total);
    }

    @Override
    public PageInfo<QryCustAccountReq> listNationalCustAccountInfo(String custId, String payName, String acountType, String acctId, String cbssAcountCode, String provinceAcctId, String payCompactNo, String provinceCode, String eparchyCode, String userCode, int pageNum, int pageSize) {
        String resultMap=this.getNationalCustAccountInfo(custId,payName,acountType,acctId,cbssAcountCode,provinceAcctId,payCompactNo,provinceCode,eparchyCode, userCode,pageNum,pageSize);
        PageInfo<QryCustAccountReq> custAccountList = LocalTokenUtil.checkRespParamAndGetDataListPage(resultMap, "QRY_ACCT_INTERNATIONAL_INFO_LIST_RSP", QryCustAccountReq.class, pageSize, pageNum);
        return  custAccountList;
    }

    @Override
    public Map<String, Object> sysncNationAccount(CuccContextCustDTO custDto, String payName, String accountType,
                                                  String accountCycle, String payCycle, String acctContactName, String acctContactPhone, String acctContacatEmail,
                                                  String acctContactAddr, String bindPostAddr, String isCenterDirectSign, String billType,
                                                  String billCycle, String billContent, String billEmail, String faxNumber, String recipientName,
                                                  String billAddress, String postCode, String remark, String bankAcctName, String bankAcctNo,
                                                  String bankCode, String superBankCode, String limitMode, String limitMoney, String agreementNo,
                                                  String fdContactName, String fdContactPhone,String state, String fdContactEmail, String currency, String postLang, String accDetailType) {

        IOrderQueryService orderQueryService = SpringUtil.getBean(IOrderQueryService.class);
        InfService infService = orderQueryService.queryServiceBySvcCode(
            CallClientEnum.SYNC_NATIONAL_CUSTOMER_ACCT.getServiceCode(), CallClientEnum.SYNC_NATIONAL_CUSTOMER_ACCT.getCenterCode());

        if (infService == null) {
            ErrorConsts.SERVICE_UNREGISTERED.throwOut(
                CallClientEnum.SYNC_NATIONAL_CUSTOMER_ACCT.getCenterCode() + ":" + CallClientEnum.SYNC_NATIONAL_CUSTOMER_ACCT
                    .getServiceCode());
        }

        Map<String, Object> returnMap = new HashMap<String, Object>();
        LoginInfo loginInfo = ContextUtil.getLoginInfo();

//        Map<String, Object> param = new HashMap<>();
        CustAccountDTO custAccountDTO = new CustAccountDTO();
//
        custAccountDTO.setCustId(Long.valueOf(custDto.getCustId())); // 必填
        custAccountDTO.setGroupId(custDto.getGroupId());
        custAccountDTO.setCustName(custDto.getCustName());
        custAccountDTO.setPayName(payName);
        custAccountDTO.setPayModeCode(accountType);
        custAccountDTO.setIsGroupAcct("1");
        //所属区域
        // custDto获取不到时取登录信息省份地市信息
        if (KiteStringUtils.isEmpty(custDto.getProvinceCode())) {
            custAccountDTO.setProvinceCode(loginInfo.getUserInfo().getPostProvinceNbr());
        } else {
            custAccountDTO.setProvinceCode(custDto.getProvinceCode());
        }
        if (KiteStringUtils.isEmpty(custDto.getEparchyCode())) {
            custAccountDTO.setEparchyCode(loginInfo.getUserInfo().getPostLanId());
        } else {
            custAccountDTO.setEparchyCode(custDto.getEparchyCode());
        }
        custAccountDTO.setAccountCycle(accountCycle);
        custAccountDTO.setPayCycle(payCycle);
        custAccountDTO.setAcctContactName(acctContactName);
        custAccountDTO.setAcctContactPhone(acctContactPhone);
        custAccountDTO.setAcctContactEmail(acctContacatEmail);
        custAccountDTO.setAcctContactAddr(acctContactAddr);
        custAccountDTO.setIsCenterDirectSign(isCenterDirectSign);
        custAccountDTO.setRemark(remark);

        //是否邮寄为非必填字段，如果前台不选择默认为不邮寄
//        bindPostAddr = KiteStringUtils.isNotEmpty(bindPostAddr) ? bindPostAddr : "0";
//
        custAccountDTO.setBindPostAddr(bindPostAddr);

        Map<String, Object>  syncInteNationalAcctReq = new HashMap<>();
        Map<String, Object> operUserInfo = new HashMap<>();
        String operNo = (String) loginInfo.getUserInfo().getExtParams().get("account");//传参改为集客工号
        operUserInfo.put("USER_CODE", KiteStringUtils.isNotEmpty(operNo)?operNo:loginInfo.getStaffInfo().getStaffCode());
        operUserInfo.put("STAFF_ID", "");

        Map<String, Object> tcpContMap = new HashMap<>();
        tcpContMap.put("APP_KEY", KiteCartInfConsts.APP_ID);

        syncInteNationalAcctReq.put("OPER_USER_INFO", operUserInfo);
        syncInteNationalAcctReq.put("TCP_CONT", tcpContMap);
        //操作类型
        syncInteNationalAcctReq.put("OPR_TYPE", "0");
        //国际账户编号
        syncInteNationalAcctReq.put("PROVINCE_ACCT_ID", KiteCartShoppingCartUtil.getRandomProvinceAcctId());
        //支付方式
        syncInteNationalAcctReq.put("PAY_WAY", custAccountDTO.getPayCycle());
        //是否需要明细账单
        syncInteNationalAcctReq.put("ACC_DETAIL_TYPE",accDetailType);
        //财务联系人
        syncInteNationalAcctReq.put("FINC_CONTACT", fdContactName);
        //财务联系电话
        syncInteNationalAcctReq.put("FINC_CONTACT_TEL", fdContactPhone);
        //财务联系人EMAIL
        syncInteNationalAcctReq.put("FINC_CONTACT_EMAIL", fdContactEmail);
        //账户状态
        syncInteNationalAcctReq.put("STATE", state);
        //开户银行
        syncInteNationalAcctReq.put("OPEN_BANK", bankAcctName);

        syncInteNationalAcctReq.put("BANK_ACC", bankAcctNo);

        //邮寄信息
        //账单类型
        syncInteNationalAcctReq.put("BILL_TYPE", billType);

            //账单币种
            syncInteNationalAcctReq.put("CURRENCY", currency);
            //账单邮寄语种
            syncInteNationalAcctReq.put("POST_LANG", postLang);
            //账单邮寄地址
            syncInteNationalAcctReq.put("ADDR_REGION", billAddress);


        //备注
        syncInteNationalAcctReq.put("NOTE", remark);



        //账户类型(国际账户同步接口暂时只有普通账户1和结算账户9两种，暂时默认普通账户)
        syncInteNationalAcctReq.put("ACC_TYPE", accountType);
        //账户名称
        syncInteNationalAcctReq.put("ACC_NAME", payName);
        //49 国际公司
        syncInteNationalAcctReq.put("PROVINCE_CODE", custDto.getProvinceCode());
        //
        syncInteNationalAcctReq.put("EPARCHY_CODE", custDto.getEparchyCode());
        //创建日期
        syncInteNationalAcctReq.put("CREATE_DATE", DateUtils.getDateString());
        //创建工号
        syncInteNationalAcctReq.put("CREATE_LOGIN_NO", loginInfo.getUserInfo().getUserCode());
        //FEE_TYPE缴费类型默认0
        syncInteNationalAcctReq.put("FEE_TYPE", "0");
        //国际客户编号
        syncInteNationalAcctReq.put("PROVINCE_CUST_ID", custDto.getCustId());
        Map<String, Object> uniBssBodyMap = new HashMap<>();
        uniBssBodyMap.put("SYNC_INTERNATIONAL_ACCT_REQ", syncInteNationalAcctReq);
//        JSONObject uniBssBody = JsonUtils.transKeyToUpperCaseObject(JSONObject.toJSON(uniBssBodyMap).toString());
//        Map<String, Object> request = new HashMap<>();
//        request.put("UNI_BSS_BODY", uniBssBody);
//        request.put("OP_CODE", " json_jike_natureCustCenter_syncInternationalAcct");

        Map<String, Object> param = LocalTokenUtil.getParamMap(uniBssBodyMap);
        param.put("OP_CODE","json_jike_natureCustCenter_syncInternationalAcct");
        InfServiceHelper.adaptGray(param, syncInteNationalAcctReq);
        String resultStr;
        if (KiteStringUtils.isEqual(mode, "http")) {
            resultStr = HttpsCallClient.postStringWithObject(LocalCallClientEnum.INF_COMMON_URL, param, LocalTokenUtil.getHeaderMap());
        }
        else {
            InfExchangeService infExchangeService = SpringUtil.getBean("infExchangeService");
            resultStr = infExchangeService.exchange(new JSONObject(param));
        }
        if (KiteStringUtils.isEmpty(resultStr)) {
            ErrorConsts.INF_INVOKE_ERROR.throwOut(LocalCallClientEnum.INF_COMMON_URL.getServiceCode(), "同步国际账户资料信息",
                "同步国际账户资料信息失败");
        }

//        String result = HttpsCallClient.postStringWithObject(LocalCallClientEnum.INF_COMMON_URL, request, null);
//        if (KiteStringUtils.isBlank(result)) {
//            ErrorConsts.BUSI_COMMON_ERROR.throwOut("同步国际账户资料返回参数为空");
//        }
        Map<?, ?> resultMap = LocalTokenUtil.checkRespParamAndGetObject(resultStr, "SYNC_INTERNATIONAL_ACCT_RSP", Map.class);
        if (!KiteStringUtils.equals(KiteMapUtils.getString(resultMap, "RESULT_CODE"), LocalInfConsts.UNI_BSS_BODY_RESULT_SUCCESS)) {
            ErrorConsts.INF_INVOKE_EXCEPTION.throwOut("json_jike_natureCustCenter_syncInternationalAcct",KiteMapUtils.getString(resultMap, "RESULT_MSG"));
        }
        returnMap.put("RESULT_CODE", "0000");
        return returnMap;
    }

    private String getNationalCustAccountInfo(String custId, String payName, String acountType, String acctId, String cbssAcountCode, String provinceAcctId, String payCompactNo, String provinceCode, String eparchyCode, String userCode, int pageNum, int pageSize) {
        IOrderQueryService orderQueryService = SpringUtil.getBean(IOrderQueryService.class);
        InfService infService = orderQueryService.queryServiceBySvcCode(
            CallClientEnum.QRY_NATIONAL_CUSTOMER_ACCTLIST.getServiceCode(), CallClientEnum.QRY_NATIONAL_CUSTOMER_ACCTLIST.getCenterCode());

        if (infService == null) {
            ErrorConsts.SERVICE_UNREGISTERED.throwOut(
                CallClientEnum.QRY_NATIONAL_CUSTOMER_ACCTLIST.getCenterCode() + ":" + CallClientEnum.QRY_NATIONAL_CUSTOMER_ACCTLIST
                    .getServiceCode());
        }

        Map<String, Object> uniBssBody = new HashMap<>();
        Map<String, Object> qryNationalCustAccountListReq = new HashMap<>();
        Map<String, String> tcpCont = new HashMap<>();
        //当APP_KEY为Z时,接口增加返回"是否总部直签"
        String headSign = DcPublicCache.getCodea("20201216002", "qryAccountInfo");
        if (KiteStringUtils.equals("T", headSign)) {
            tcpCont.put("APP_KEY", LocalInfConsts.APP_ID_Z);
        } else {
            tcpCont.put("APP_KEY", LocalInfConsts.APP_ID);
        }
        tcpCont.put("TRANSACION_ID", LocalTokenUtil.getTransId());
        qryNationalCustAccountListReq.put("TCP_CONT", tcpCont);

        Map<String, Object> operUserInfo = new HashMap<>();
//        operUserInfo.put("STAFF_ID", "");
        if (KiteStringUtils.isEmpty(userCode)) {
            operUserInfo.put("USER_CODE", KiteStringUtils.valueOf(ContextUtil.getSimpleLoginInfo().getUserCode())); //必填 用户账号
        } else {
            operUserInfo.put("USER_CODE", userCode);
        }
//        operUserInfo.put("ROLE_ID", "");
//        operUserInfo.put("LOGIN_ROLE_AREA_CODE", "");
        qryNationalCustAccountListReq.put("OPER_USER_INFO", operUserInfo);

        qryNationalCustAccountListReq.put("PAGE_SIZE", pageSize); //必填
        qryNationalCustAccountListReq.put("PAGE_NUM", pageNum); //必填
        if (!KiteStringUtils.isBlank(custId)) {
            qryNationalCustAccountListReq.put("CUST_ID", custId);
        }
        qryNationalCustAccountListReq.put("ACCT_ID", acctId);
        //todo 国际账户Id
        qryNationalCustAccountListReq.put("PROVINCE_ACCT_ID", "");
        qryNationalCustAccountListReq.put("ACOUNT_NAME", payName);
        qryNationalCustAccountListReq.put("ACOUNT_TYPE", acountType);
        qryNationalCustAccountListReq.put("CBSS_ACOUNT_CODE", cbssAcountCode);
        qryNationalCustAccountListReq.put("PROVICE_ACOUNT_CODE", provinceAcctId);
        qryNationalCustAccountListReq.put("PAY_COMPACT_NO", payCompactNo);
        qryNationalCustAccountListReq.put("PROVINCE_CODE", provinceCode); //必填
        qryNationalCustAccountListReq.put("EPARCHY_CODE", eparchyCode);
        uniBssBody.put("QRY_ACCT_INTERNATIONAL_INFO_LIST_REQ", qryNationalCustAccountListReq);
        Map<String, Object> param = LocalTokenUtil.getParamMap(uniBssBody);
//        param.put("OP_CODE","json_jike_natureCustCenter_qryAcctInternationalInfoList");
        InfServiceHelper.adaptGray(param, qryNationalCustAccountListReq);
        String resultStr;
        if (KiteStringUtils.isEqual(mode, "http")) {
            resultStr = HttpsCallClient.postStringWithObject(CallClientEnum.QRY_NATIONAL_CUSTOMER_ACCTLIST, param, LocalTokenUtil.getHeaderMap());
        }
        else {
            InfExchangeService infExchangeService = SpringUtil.getBean("infExchangeService");
            resultStr = infExchangeService.exchange(new JSONObject(param));
        }
        if (KiteStringUtils.isEmpty(resultStr)) {
            ErrorConsts.INF_INVOKE_ERROR.throwOut(CallClientEnum.QRY_NATIONAL_CUSTOMER_ACCTLIST.getServiceCode(), "获取国际客户账户信息",
                "获取国际客户账户信息失败");
        }

        return  resultStr;
    }

    public static String getRandomProvinceAcctId() {
        //按照赋值规则组装 先获取当前的毫秒时间数，与一个4位的随机数（随机数不足4位则在前补0）拼接。
        Long timeStamp=System.currentTimeMillis();
        //生成四位随机数
        Random random=new Random();
        int randomNum=random.nextInt(9999);
        //若不足三位前面补0
        String result=timeStamp+String.format("%04d",randomNum);
        return  result;
    }

}
